package com.aliyun;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

public class ShiroTest {
    public static void main(String[] args) {
        //根据安全管理工厂获取安全管理器
        IniSecurityManagerFactory iniSecurityManagerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = iniSecurityManagerFactory.getInstance();

        SecurityUtils.setSecurityManager(securityManager);
        //创建subject对象
        Subject subject =SecurityUtils.getSubject();
        AuthenticationToken authenticationToken = new UsernamePasswordToken("admin","123");

        try {
            subject.login(authenticationToken);
            System.out.println("登录成功");
            System.out.println("登录成功");

            Boolean hasRole = subject.hasRole("admin");
            System.out.println("是否有admin的角色："+hasRole);
            boolean permitted  = subject.isPermitted("user:insert");
            System.out.println("是否拥有权限："+permitted);
        }catch (UnknownAccountException e){
            e.printStackTrace();
            System.out.println("用户不存在");
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            System.out.println("密码错误");
        }


    }
}
